package t3dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import t3jdbc.JDBCUtil;
import t3bean.ParkingRecordBean;

public class ParkingRecordDao extends Dao {
	private SetSql ss = new SetSql();

	public int delete(ParkingRecordBean prb) {
		String sql = "delete from parkingrecord where ";
		List<Object> list = new ArrayList<>();
		sql = ss.setSql(prb, sql, list, 2);
		int back = 0;
		try {
			back = dml(sql, list);
			return back;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int insert(ParkingRecordBean prb) {
		String sql = "insert into parkingrecord(space_id,car_id,parkingtime) values(?,?,?)";
		List<Object> list = new ArrayList<>();
		list.add(prb.getSpaceId());
		list.add(prb.getCarId());
		list.add(prb.getParkingTime());

		int back = 0;
		try {
			back = dml(sql, list);
			return back;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<ParkingRecordBean> select(ParkingRecordBean prb) throws SQLException {
		String sql;
		List<ParkingRecordBean> backList = new ArrayList<>();
		List<Object> list = new ArrayList<>();
		ResultSet rs = null;
		if (prb != null) {
			sql = "select * from parkingrecord where ";
			sql = ss.setSql(prb, sql, list, 2);
//			System.out.println(sql);
		} else {
			sql = "select * from parkingrecord";
		}
//		System.out.println(sql);
		try {
			rs = dql(sql, list);
			while (rs.next()) {
				ParkingRecordBean prb1 = new ParkingRecordBean();
				prb1.setId(rs.getInt("id"));
				prb1.setCarId(rs.getString("car_id"));
				prb1.setSpaceId(rs.getInt("space_id"));
				Timestamp time=rs.getTimestamp("parkingtime");
				prb1.setParkingTime(time.toLocalDateTime());
				
				backList.add(prb1);
			}
			return backList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(super.conn, super.ps, super.rs);
			super.conn = null;
			super.ps = null;
			super.rs = null;
			rs.close();
			rs = null;
		}
		return null;
	}
}
